package top.go2do.common.httpResponseResult;

import java.util.Date;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.MDC;
import top.go2do.common.exception.BusinessResultCode;

@Schema(description = "全局http响应实体")
@Setter
@Getter
public class ApiResponse<T> {

    /**
     * 响应状态码
     */
    @Schema(description = "响应状态码", example = "200")
    private Integer code;

    /**
     * 响应描述
     */
    @Schema(description = "响应描述", example = "success")
    private String msg;

    /**
     * 响应数据
     */
    @Schema(description = "响应数据", example = "{}")
    private T data;

    /**
     * 请求ID
     */
    @Schema(description = "请求ID", example = "1q2w3e")
    private String requestId;

    /**
     * 请求时间
     */
    @Schema(description = "请求时间", example = "123")
    private Date requestTime;

    public ApiResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.requestId = MDC.get("requestId");
        this.requestTime = new Date();
    }

    public static <T> ApiResponse<T> success(T data) {
        BusinessResultCode resultCode = BusinessResultCode.SUCCESS;
        return new ApiResponse<>(resultCode.getCode(), resultCode.getMsgKey(), data);
    }

    public static ApiResponse<?> error(int code, String msg) {
        return new ApiResponse<>(code, msg, null);
    }

}
